/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package htmlunit.code;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 *
 * @author TiTa
 */
public class kiem_tra_loi_404 {
    
    public boolean exists(String URLName)throws MalformedURLException, IOException{
     TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
        public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType) {
        }
    }
};

// Install the all-trusting trust manager
try {
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}

// Now you can access an https URL without having the certificate in the truststore
try {
    URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {
}
  try {

    Properties systemSettings = System.getProperties();
    systemSettings.put("proxySet", "true");
    systemSettings.put("http.proxyHost","proxy.mycompany.local") ;
    systemSettings.put("http.proxyPort", "80") ;

    URL u = new URL(URLName);
    
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
  con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
 
    sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
    String encodedUserPwd =
         encoder.encode("domain\\username:password".getBytes());
    con.setRequestProperty
         ("Proxy-Authorization", "Basic " + encodedUserPwd);
    con.setRequestMethod("HEAD");
    System.out.println
         (con.getResponseCode() + " : " + con.getResponseMessage());
    return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
  catch (Exception e) {
       e.printStackTrace();
       return false;
       }
  }
}
